<?php
Yii::import('application.models.geo.GeoActiveRecord');
/**
 * This is the model class for table "public_location".
 *
 * The followings are the available columns in table 'public_location':
 * @property string $id
 * @property string $name
 * @property string $city_id
 * @property string $latlng
 *
 * The followings are the available model relations:
 * @property District[] $districts
 * @property City $city
 */
class PublicLocation extends GeoActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PublicLocation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'public_location';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
        Yii::import('application.components.validators.UniqueAttributesValidator');
		return array(
			array('name, city_id, latlng, districts', 'required'),
            array('alias', 'UniqueAttributesValidator', 'with' => 'city_id'),
			array('name', 'length', 'max'=>255),
			array('city_id', 'length', 'max'=>10),
			array('latlng', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, city_id, latlng', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'districts' => array(self::MANY_MANY, 'District', 'district_has_public_location(public_location_id, district_id)'),
			'city' => array(self::BELONGS_TO, 'City', 'city_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
            'name' => 'Tên địa điểm',
			'alias' => 'Url địa điểm',
			'city_id' => 'Thành phố/ Tỉnh',
            'latlng' => 'Tọa độ',
            'districts' => 'Quận/ Huyện',
			'status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('latlng',$this->latlng,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
    * Lấy tất cả public location nhóm theo city_id
    */
    public function getDataGroupByCity(){
        $publicLocations = $this->findAll();
        $data = array();
        foreach($publicLocations as $pl){
            list($lat, $lng) = explode(',', $pl->latlng);
            $data[$pl->city_id][$pl->alias] = array(   
                'id' => $pl->id,    
                'name' => $pl->name,    
                'lat' => $lat,    
                'lng' => $lng,    
            );     
        }
        // TODO: cache this data
        return $data; 
    }
    
    public function getStatusData(){
        return array(
            'ENABLE' => 'Hiện',
            'DISABLE' => 'Ẩn',
        );
    }
    
    public function getStatusLabel(){
        return $this->statusData[$this->status];
    }
    
    public function getDistrictLabelList(){
        $html = NULL;
        if($this->districts){
            $html = "<ul>";
            foreach($this->districts as $district){
                $html .= "<li>".$district->name."</li>";    
            }
            $html .= "</ul>";
        }
        return $html;

    }
}